﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MacomberMapSystem.Common.Serialization;
using System.Xml;
using System.Data.Common;
using MacomberMapSystem.Common.Database;

namespace MacomberMapSystem.Common.Integration.Blackstart
{
    /// <summary>
    /// This class holds information on a blackstart corridor
    /// </summary>
    public class MM_Blackstart_Corridor: MM_Serializable, IComparable
    {
        #region Variable declarations
        /// <summary>The name of our corridor</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public String Name;

        /// <summary>The description name of our corridor</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public String Description;

        /// <summary>The notes about our corridor</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public String Notes;

        /// <summary>The Xml configuration of our corridor.</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]        
        public XmlDocument Xml;

        /// <summary>Our collection of blackstart corridor paths</summary>
        public List<MM_Blackstart_Corridor_Target> CorridorTargets = new List<MM_Blackstart_Corridor_Target>();

        /// <summary>The models for which this item is identified</summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue)]
        public MM_Database_Model[] Models;
        #endregion

        #region Initialization
        /// <summary>
        /// Initialize a new blackstart corridor from our repository
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="Model"></param>
        public MM_Blackstart_Corridor(XmlDocument xDoc, MM_Database_Model Model)
            : base(xDoc.DocumentElement, Model)
        {
            this.Xml = xDoc;
            foreach (XmlElement xTarget in xDoc.DocumentElement.ChildNodes)
                CorridorTargets.Add(new MM_Blackstart_Corridor_Target(xTarget, Model));            
        }

        /// <summary>
        /// Initialize a new Blackstart Corridor
        /// </summary>
        /// <param name="dRd"></param>
        /// <param name="xDoc"></param>
        /// <param name="Repository"></param>
        public MM_Blackstart_Corridor(DbDataReader dRd, XmlDocument xDoc, MM_Repository Repository)
            : base(dRd, xDoc, Repository)
        {
            foreach (XmlElement xTarget in Xml.DocumentElement.ChildNodes)
                CorridorTargets.Add(new MM_Blackstart_Corridor_Target(xTarget, null));          
        }

       
        /// <summary>
        /// Merge our model information with our corridor
        /// </summary>
        /// <param name="Model"></param>
        public void AssignModel(MM_Database_Model Model)
        {
            foreach (MM_Blackstart_Corridor_Target Target in CorridorTargets)
                Target.AssignModel(Model);
        }
        #endregion

        /// <summary>
        /// Report the name of our corridor
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return Name;
        }

        /// <summary>
        /// Allow comparisons of the blackstart corridors
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int CompareTo(object obj)
        {
            if (obj is MM_Blackstart_Corridor == false)
                throw new NotImplementedException();
            int Comp= Name.CompareTo((obj as MM_Blackstart_Corridor).Name);
            if (Comp != 0)
                return Comp;
            else
                return this.ID.CompareTo((obj as MM_Blackstart_Corridor).ID);
        }
    }
}
